package com.jk.kdd.system.service;

import com.github.pagehelper.PageHelper;
import com.jk.kdd.system.dao.SystemRoleMapper;
import com.jk.kdd.system.dao.SystemUserMapper;
import com.jk.kdd.system.dao.SystemUserRoleMapper;
import com.jk.kdd.system.model.SystemRole;
import com.jk.kdd.system.model.SystemUser;
import com.jk.kdd.system.model.SystemUserRole;
import com.jk.kdd.system.vo.SystemRoleVO;
import com.jk.kdd.system.vo.SystemUserVO;
import com.jk.util.BaseDataUtil;
import org.smart4j.framework.helper.BeanHelper;
import org.smart4j.framework.util.CastUtil;
import org.smart4j.framework.util.CollectionUtil;
import org.smart4j.plugin.security.util.SecurityUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import util.BeanUtil;
import util.PagedResult;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created by DELL on 2016/10/9.
 */
@Service("systemUserService")
public class SystemUserServiceImpl implements  SystemUserService{

    @Resource
    private SystemUserMapper systemUserMapper;
    @Resource
    private SystemRoleMapper systemRoleMapper;

    /**
     * 获取实体列表
     */
    public List<SystemUserVO> getSystemUserList() {
        List<SystemUser> userList = systemUserMapper.selectAll();
        for (int i = 0; i < userList.size(); i++) {
            List<SystemRole> roleList = systemRoleMapper.selectByUserId(userList.get(i).getId());
            userList.get(i).setRoleList(roleList);
        }
        List<SystemUserVO> promotionAdInfoVOList = BeanHelper.mapList(userList,SystemUserVO.class);
        return promotionAdInfoVOList;
    }
    /**
     * 根据主键获取实体
     */
    public SystemUserVO getSystemUserByPK(SystemUser user) {
        user = systemUserMapper.selectByPrimaryKey(user.getId());
        List<SystemRole> roleList = systemRoleMapper.selectByUserId(user.getId());
        user.setRoleList(roleList);
        SystemUserVO systemUserVO = new SystemUserVO();
        BeanHelper.copyBean(user,systemUserVO);
        return systemUserVO;
    }

    @Override
    public SystemUser getSystemUserModelByPK(SystemUser user) {
        user = systemUserMapper.selectByPrimaryKey(user.getId());
        List<SystemRole> roleList = systemRoleMapper.selectByUserId(user.getId());
        user.setRoleList(roleList);
        return user;
    }
    /**
     * 根据username获取用户
     */
    @Override
    public SystemUserVO getSystemUserByUsername(String username) {

        SystemUser systemUser = systemUserMapper.selectUserByUsername(username);
        SystemUserVO vo = new SystemUserVO();
        BeanHelper.copyBean(systemUser,vo);
        return vo;
    }

    /**
     * 取得自己管理的用户
     */
    @Override
    public PagedResult<SystemUserVO> getManageSystemUser(String operatorId, String pageNum, String pageSize) {

        List<SystemUser> systemUserList = new ArrayList<SystemUser>();

        PageHelper.startPage(Integer.parseInt(pageNum),Integer.parseInt(pageSize));
        // 管理员管理所有账号
        if (SecurityUtil.handleHasRoles("admin")) {
            systemUserList = systemUserMapper.selectAll();
        }
        // 客服管理自己创建的账号
        else if (SecurityUtil.handleHasRoles("support")) {
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("operatorId",operatorId);
            paramMap.put("roleName","advertiser");
            systemUserList = systemUserMapper.selectUserByManageUserIdAndRoleName(paramMap);
        }

        PagedResult pagedResult = BeanUtil.toPagedResult(systemUserList,SystemUserVO.class);
        // 查询职位信息
        if (CollectionUtil.isNotEmpty(pagedResult.getDataList())) {
            for (int i = 0; i < pagedResult.getDataList().size(); i++) {
                List<SystemRole> roleList = systemRoleMapper.selectByUserId(systemUserList.get(i).getId());
                List<SystemRoleVO> systemRoleVOList = BeanHelper.mapList(roleList,SystemRoleVO.class);
                SystemUserVO userVO = (SystemUserVO) pagedResult.getDataList().get(i);
                userVO.setRoleList(systemRoleVOList);
            }
        }

        return pagedResult;
    }


    /**
     * 取得自己管理的广告主
     */
    @Override
    public List<SystemUserVO> getManageAdSystemUser(String operatorId) {

        List<SystemUser> systemUserList = new ArrayList<SystemUser>();

        // 管理员取得所有
        if (SecurityUtil.handleHasRoles("admin")) {
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("roleName","advertiser");
            systemUserList.addAll(systemUserMapper.selectUserByManageUserIdAndRoleName(paramMap));
        }
        // 客服取得自己管理
        if (SecurityUtil.handleHasRoles("support")) {
            Map<String,Object> paramMap = new HashMap<>();
            paramMap.put("operatorId",operatorId);
            paramMap.put("roleName","advertiser");
            systemUserList.addAll(systemUserMapper.selectUserByManageUserIdAndRoleName(paramMap));
        }
        // 广告主取得自己
        if (SecurityUtil.handleHasRoles("advertiser")){
            systemUserList.add(systemUserMapper.selectByPrimaryKey(CastUtil.castLong(operatorId)));
        }
        List<SystemUserVO> tagVOList = BeanHelper.mapList(systemUserList,SystemUserVO.class);
        return tagVOList;
    }


    /**
            * 判断广告主是否归自己管
     */
    public boolean isOwnManageAdUser(String userId, String operateId) {
        //取得自己管理的广告主
        List<SystemUserVO> userList = getManageAdSystemUser(operateId);

        boolean flag = false;

        for (int i = 0; i < userList.size(); i++) {
            if (userList.get(i).getId() == CastUtil.castLong(userId)) flag = true;
        }

        return flag;
    }

    /**
     * 判断用户是否归自己管
     */
    public boolean isOwnManageUser(String userId, String operateId) {
        PagedResult<SystemUserVO> userList = getManageSystemUser(operateId,"0","0");

        boolean flag = false;

        for (int i = 0; i < userList.getDataList().size(); i++) {
            if (userList.getDataList().get(i).getId() == CastUtil.castLong(userId)) flag = true;
        }

        return flag;
    }

    /**
     * 判断用户是否有进入前台权限
     */
    public boolean isCanIntoFront(String operatorId) {
        List<SystemUserVO> adUserList = getManageAdSystemUser(operatorId);
        return CollectionUtil.isNotEmpty(adUserList);
    }
}
